package com.stylefeng.guns.rest.modular.auth.util;


import com.alibaba.fastjson.JSON;

import java.security.MessageDigest;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author: A Sang
 * @Description:
 * @Date: Created in 7:11 PM 2018/11/3
 * @Modified By:
 */
public class ResultUtil {



    /** 
    * 私钥加密
    */
    public static String privateEncrypt(Object object){
        String json = JSON.toJSONString(object);
        return RSAEncrypt.privateEncrypt(json);
    }

    /**
     * 公钥解密
     */
    public static String publicDecrypt(String string){
        return RSAEncrypt.publicDecrypt(string);
    }

    /**
     * 公钥加密
     */
    public static String publicEncrypt(Object object){
        String json = JSON.toJSONString(object);
        return RSAEncrypt.publicEncrypt(json);
    }
    /**
     * 私钥解密
     */
    public static String privateDecrypt(String string){
        return RSAEncrypt.privateDecrypt(string);
    }



    public static Map<String,Object> success(Object object){

        Map<String,Object> data = new HashMap<>();
        data.put("code",200);
        data.put("msg","OK");
        data.put("data",object);
        Map<String,Object> map = new HashMap<>();
        map.put("result",privateEncrypt(data));
        //map.put("result",data);
        return map;
    }

    public static Map<String,Object> success(String msg){

        Map<String,Object> data = new HashMap<>();
        data.put("code",200);
        data.put("msg",msg);
        data.put("data",null);
        Map<String,Object> map = new HashMap<>();
        map.put("result",privateEncrypt(data));
        //map.put("result",data);
        return map;
    }

    public static Map<String,Object> info(String msg,Integer code){

        Map<String,Object> data = new HashMap<>();
        data.put("code",code);
        data.put("msg",msg);
        data.put("data",null);
        Map<String,Object> map = new HashMap<>();
        map.put("result",privateEncrypt(data));
        //map.put("result",data);
        return map;
    }

    public static Object error(String msg){
        Map<String,Object> data = new HashMap<>();
        data.put("code",101);
        data.put("msg",msg);
        data.put("data",null);
        Map<String,Object> map = new HashMap<>();
        map.put("result",privateEncrypt(data));
        //map.put("result",data);
        return map;
    }


    public static String getSha1(String str){
        if(str==null||str.length()==0){
            return null;
        }
        char hexDigits[] = {'0','1','2','3','4','5','6','7','8','9',
                'a','b','c','d','e','f'};
        try {
            MessageDigest mdTemp = MessageDigest.getInstance("SHA1");
            mdTemp.update(str.getBytes("UTF-8"));

            byte[] md = mdTemp.digest();
            int j = md.length;
            char buf[] = new char[j*2];
            int k = 0;
            for (int i = 0; i < j; i++) {
                byte byte0 = md[i];
                buf[k++] = hexDigits[byte0 >>> 4 & 0xf];
                buf[k++] = hexDigits[byte0 & 0xf];
            }
            return new String(buf);
        } catch (Exception e) {
            return null;
        }
    }

}
